import { request } from '@/utils/request';

// 后端基础URL - 使用相对路径，通过Vite代理
const BASE_URL = '';

/**
 * 简化版任务发布接口
 */
export const publishSimpleTask = (data: any) => {
  console.log('发布任务请求数据:', data);
  console.log('请求URL:', `${BASE_URL}/drone/taskSimple/publish`);
  
  return request({
    url: `/drone/taskSimple/publish`,
    method: 'POST',
    data: data,
    header: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    },
    loadTime: true // 显示加载动画
  });
};

/**
 * 查询可接任务（已发布任务）
 */
export const getPublishedTasks = (params?: any) => {
  console.log('请求可接任务列表，参数:', params);
  
  // GET请求的参数应该通过URL查询字符串传递
  let url = `/drone/task/published`;
  if (params) {
    const queryString = Object.keys(params)
      .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
      .join('&');
    url += `?${queryString}`;
  }
  
  console.log('请求URL:', url);
  
  return request({
    url: url,
    method: 'GET',
    loadTime: false
  });
};

/**
 * 根据分类ID查询已发布的任务列表
 */
export const getTasksByCategory = (categoryId: number, params?: any) => {
  console.log('根据分类查询任务，分类ID:', categoryId, '参数:', params);
  
  // GET请求的参数应该通过URL查询字符串传递
  let url = `/drone/taskCategory/tasks/${categoryId}`;
  if (params) {
    const queryString = Object.keys(params)
      .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
      .join('&');
    url += `?${queryString}`;
  }
  
  console.log('请求URL:', url);
  
  return request({
    url: url,
    method: 'GET',
    loadTime: false
  });
};

/**
 * 查询所有已发布的任务列表（不限制分类）
 */
export const getAllPublishedTasks = (params?: any) => {
  console.log('查询所有已发布任务，参数:', params);
  
  // GET请求的参数应该通过URL查询字符串传递
  let url = `/drone/taskCategory/tasks`;
  if (params) {
    const queryString = Object.keys(params)
      .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
      .join('&');
    url += `?${queryString}`;
  }
  
  console.log('请求URL:', url);
  
  return request({
    url: url,
    method: 'GET',
    loadTime: false
  });
};

/**
 * 飞手接单（带权限检查）
 */
export const acceptTask = (taskId: number) => {
  console.log('飞手接单，任务ID:', taskId);
  console.log('请求URL:', `/drone/task/accept/${taskId}`);
  return request({
    url: `/drone/task/accept/${taskId}`,
    method: 'POST',
    loadTime: true
  });
};

/**
 * 查看我的任务详情
 */
export const getMyTasks = (params?: any) => {
  console.log('查看我的任务详情，参数:', params);
  
  // GET请求的参数应该通过URL查询字符串传递
  let url = `/drone/task/myTasks`;
  if (params) {
    const queryString = Object.keys(params)
      .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
      .join('&');
    url += `?${queryString}`;
  }
  
  console.log('请求URL:', url);
  
  return request({
    url: url,
    method: 'GET',
    loadTime: false
  });
};
